{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建对象时调用\n",
      "创建对象时调用\n",
      "1764995430672\n",
      "1764995430192\n",
      "张三\n",
      "18\n",
      "张三在吃东西\n",
      "test\n",
      "test\n",
      "test2\n",
      "王五在睡觉,不高兴了\n",
      "王五在睡觉,赵六不高兴了\n",
      "张三是男生\n",
      "张三是女生\n",
      "张三对象被删除了\n",
      "程序结束\n"
     ]
    }
   ],
   "source": [
    "#面向对象编程\n",
    "#类和对象，类是抽象的模板，对象是具体的实例\n",
    "#封装\n",
    "# 1、XXX:普通属性和方法 \n",
    "# 2、_XXX:私有属性和方法 如果定义在类中，外部可以使用，但是 from 模块名 import * 无法导入，不能访问，一般是为了避免与关键字冲突\n",
    "# 3、__XXX:隐藏属性和方法 不能在类外部访问 只能在类内部访问；from 模块名 import * 无法导入，不能访问\n",
    "class Person:\n",
    "    #类属性\n",
    "    name=\"\"\n",
    "    age=0\n",
    "    __sex=\"\"#隐藏属性 不能在类外部访问 只能在类内部访问\n",
    "    #构造方法\n",
    "    def __init__(self,name,age):# 初始化对象\n",
    "        #实例属性\n",
    "        self.name=name\n",
    "        self.age=age\n",
    "        self.__sex=\"男\"\n",
    "    def __new__(cls,*args,**kwargs):#创建对象时调用 比init更早,重新了顶级类的方法，就不会执行__init__\n",
    "        print(\"创建对象时调用\")\n",
    "        return super().__new__(cls) #返回对象 这样就会执行__init__ 重写__new__ 一定要调用super().__new__() 这样创建一个新的对象然后执行__init__进行初始化\n",
    "\n",
    "    #析构函数 删除对象（对象执行结束后）时默认调用此方法\n",
    "    def __del__(self):\n",
    "        print(f\"{self.name}对象被删除了\")\n",
    "    #实例方法\n",
    "    def eat(self):\n",
    "        print(\"{}在吃东西\".format(self.name))\n",
    "    #实例属性  实例属性只能由实例访问，不能由对象访问\n",
    "    def sleep(self):\n",
    "        print(f\"{self.name2}在睡觉,{Person.name}不高兴了\")\n",
    "    def mysex(self):\n",
    "        print(f\"{self.name}是{self.__sex}生\")\n",
    "    #静态方法 不需要实例对象，不需要self cls 可以以类名.方法名调用，也可以使用对象访问，不需要访问实例属性和类属性\n",
    "    @staticmethod\n",
    "    def test():\n",
    "        print(\"test\")\n",
    "    @staticmethod #带参数的静态方法\n",
    "    def testparam(param):\n",
    "        print(f\"{param}test\")\n",
    "    #类方法\n",
    "    @classmethod # 以类作为参数，以cls作为参数,不能访问实例属性\n",
    "    #cls.name 可以访问类属性\n",
    "    def test2(cls):\n",
    "        print(f\"{cls.name}test2\")\n",
    "#创建对象\n",
    "p=Person(\"张三\",18)\n",
    "p1=Person(\"李四\",20)\n",
    "print(id(p))\n",
    "print(id(p1))\n",
    "#访问属性\n",
    "print(p.name)\n",
    "print(p.age)\n",
    "#调用方法\n",
    "p.eat()\n",
    "#调用静态方法\n",
    "Person.test()\n",
    "p.test() #test\n",
    "#调用类方法\n",
    "Person.test2()\n",
    "#实例属性\n",
    "p.name2=\"王五\"\n",
    "p.sleep() #王五在睡觉, 不高兴了 Person.name 没赋值\n",
    "Person.name=\"赵六\"\n",
    "p.sleep() #王五在睡觉, 赵六不高兴了\n",
    "p.__sex=\"女\" #改了不生效\n",
    "p.mysex() #张三是男生\n",
    "p._Person__sex=\"女\" #改了生效\n",
    "p.mysex() #张三是女生\n",
    "#print(Person.name2)#报错  实例属性只能由实例访问，不能由对象访问 AttributeError: type object 'Person' has no attribute 'name2'\n",
    "del p # 删除李三对象 李四未删除，程序结束后删除\n",
    "print(\"程序结束\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0rc2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
